package tiled.io;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import tiled.core.Tile;
import tiled.util.NumberedSet;

public class ImageInOne {

	NumberedSet oldTile = new NumberedSet();

	BufferedImage imgAll = null;
	Vector<BufferedImage> imgLsit = new Vector<BufferedImage>();
	int count;
	
	public ImageInOne(){
		
	}
	
	public int add(Tile tile) throws IOException {
		if(oldTile == null || tile==null  ) return -1;
		
		if(oldTile.contains(tile)) return oldTile.indexOf(tile);
		System.out.println(tile.toString());
		oldTile.add(tile);
		add(tile.getImage());
		
		return oldTile.indexOf(tile);
	}
	
	public void add(Image img) throws IOException {
		BufferedImage bImg = ImageHelper.imageToBuffer(img);
		imgLsit.add(bImg);
		count++;
	}
	
	public void end(){
		int width = ((Tile)oldTile.get(0)).getWidth();
		int height = ((Tile)oldTile.get(0)).getHeight();
		imgAll =  new   BufferedImage(width*count,height,BufferedImage.TYPE_INT_RGB); 
		Iterator<BufferedImage>it = imgLsit.iterator();
		int i=0;
		while(it.hasNext()){
			int[]  ImageArray   =   new   int[width*height];   
			ImageArray = it.next().getRGB(0, 0, width, height, ImageArray, 0, width);
			imgAll.setRGB(width*i, 0, width, height, ImageArray, 0, width);
			i++;
		}
	}
	
	
	public int getCount() {
		return count;
	}

	public BufferedImage getImgAll() {
		end();
		return imgAll;
	}

	public void setImgAll(BufferedImage imgAll) {
		this.imgAll = imgAll;
	}
	
}
